package com.ygxsk.carhome.ui.complain;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gyf.immersionbar.ImmersionBar;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantApi;
import com.ygxsk.carhome.response.CustomerTitle;
import com.ygxsk.carhome.ui.ErrorActivity;

import java.util.ArrayList;

import butterknife.BindView;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-08-04  08:56
 * @Description:投诉处理 的选项卡切换页面，是 参考 的管家物业的来的
 */
public class ComplaintHandlerTabActivity extends ErrorActivity {

    //装载的fragment
    private ArrayList<Fragment> fragmentLists = new ArrayList<>();

    //装载标题内容
    private ArrayList<CustomerTitle> titleLists = new ArrayList<>();

    //该界面和标题的绑定
    private TabLayoutMediator tabLayoutMediator;

    @BindView(R.id.tab_layout_complain_customer)
    TabLayout tabLayoutInComplainCustomer;

    @BindView(R.id.viewpagerincomplaincustomer)
    ViewPager2 viewPagerInComplainCustomer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivityInstance = this;

//        initloading();

        initViews();

        //其他的类似 监听器的，提交一个客户

        //监听单个客户内容

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_complaint_handler_tab;
    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        //这个可能是动态的，不同身份 是不同标题：服务站经理存款业绩排名
        toolbarHelper.setTitle(getString(R.string.title_complain_handler));
        toolbarHelper.setLayoutClose(false, "", null);
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                methodBack();
            }
        });

        toolbarHelper.setLayoutRight(false, 0, "", null);

//        toolbarHelper.setLayoutRight(true, 1, "添加", new ClickUtils.OnDebouncingClickListener() {
//            @Override
//            public void onDebouncingClick(View v) {
//                //历史记录的页面布局
//                Bundle extra_bundle = new Bundle();
//                //这里空携带
//                ActivityUtils.startActivity(extra_bundle, RunManOpenCardEditActivity.class);
//            }
//        });

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();

//        BarUtils.setStatusBarLightMode(this, false);
    }

    @Override
    protected void onClickNodata(View v) {

        //这里没有

    }

    @Override
    protected void initViews() {

        //初始化fragments
        if (fragmentLists == null) {
            fragmentLists = new ArrayList<>();
        }

        if (titleLists == null) {
            titleLists = new ArrayList<>();
        }

        CustomerTitle customerTitle_assigned = new CustomerTitle();
        customerTitle_assigned.setTitleName(getResources().getString(R.string.complain_pending_handler));

        CustomerTitle customerTitle_confirmed = new CustomerTitle();
        customerTitle_confirmed.setTitleName(getResources().getString(R.string.complain_had_handler));

//        CustomerTitle customerTitle_fight_fail = new CustomerTitle();
//        customerTitle_fight_fail.setTitleName(ConstantSign.LEAD_FIGHT_FAIL);//战败 失败
//        CustomerTitle customerTitle_fight_success = new CustomerTitle();
//        customerTitle_fight_success.setTitleName(ConstantSign.LEAD_FIGHT_SUCCESS);//战败 成功

        titleLists.add(customerTitle_assigned);
        titleLists.add(customerTitle_confirmed);
//        titleLists.add(customerTitle_fight_fail);
//        titleLists.add(customerTitle_fight_success);

        fragmentLists.add(ComplaintFeedBackOrHandlerFragment.newInstance(0, "0", //
                getResources().getString(R.string.complain_pending_handler), ConstantApi.VALUE_JUMPFROM_HOMEMAINPAGE_COMPLAINTHANDLER_CLICK817));
        fragmentLists.add(ComplaintFeedBackOrHandlerFragment.newInstance(1, "1", //
                getResources().getString(R.string.complain_had_handler), ConstantApi.VALUE_JUMPFROM_HOMEMAINPAGE_COMPLAINTFEEDBACK_CLICK819));

//        fragmentLists.add(OrderListFragment.newInstance());
//        fragmentLists.add(OrderListFragment.newInstance());

        MyComplainCustomerAdapter myTimeCustomerAdapter = new MyComplainCustomerAdapter(this, fragmentLists);
        viewPagerInComplainCustomer.setAdapter(myTimeCustomerAdapter);

        //设置字体颜色，第一个参数代表未点击时的字体颜色，第二个参数代表点击后的颜色
        //tabLayoutInCustomer.setTabTextColors(R.color.black,R.color.teal_700);
        //点击单个tab时背景为透明色
        //tabLayout.setTabRippleColor(ColorStateList.valueOf(Color.TRANSPARENT));

        //这种是不是将来还需要接口回来，重新绑定
        //让viewpager2与tablayout关联，实现滑动效果（滑动fragment底部tab变化，tab滑动fragment跟着滑动）
        tabLayoutMediator = new TabLayoutMediator(tabLayoutInComplainCustomer, viewPagerInComplainCustomer, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //标题和数量的合并
                tab.setText(titleLists.get(position).getTitleName());
            }
        });

        tabLayoutMediator.attach();//一定要attach（）

        tabLayoutInComplainCustomer.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

//                LogUtils.d(ConstantSign.LOGCAT_NOMAL+"onTabSelected--onTabSelected-->"+tab.getPosition());
                viewPagerInComplainCustomer.setCurrentItem(tab.getPosition(), false);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    @Override
    protected void gainDatas() {

        //retain

    }

    @Override
    protected void methodBack() {

        methodBackFinish();

    }


    /*
     * @Author:itchenqi175@163.com
     * @Time:2023-03-08  08:31
     * @Description:投诉处理的 左右两页的选项卡 适配器
     */
    private class MyComplainCustomerAdapter extends FragmentStateAdapter {
        private ArrayList<Fragment> fragmentList;

        public MyComplainCustomerAdapter(@NonNull FragmentActivity fragmentActivity, ArrayList<Fragment> fragments) {
            super(fragmentActivity);
            this.fragmentList = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getItemCount() {
            return fragmentList.size();
        }

    }

}